﻿package com.msjt.action;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.json.annotations.JSON;

import com.msjt.base.BaseAction;
import com.msjt.entity.Users;
import com.msjt.service.IUsersService;
import com.msjt.util.MyDate;
import com.msjt.util.PageMode;

/**
 * name:UsersAction function:用户模块Action author: kw time:2013.8.28 updateTime:
 */
@SuppressWarnings("serial")
public class UsersAction extends BaseAction {
	private Users users;
	@Resource
	private IUsersService usersService;
	// page Model
	private PageMode page;
	private String checkMsg;
	
	public String getCheckMsg() {
		return checkMsg;
	}

	public void setCheckMsg(String checkMsg) {
		this.checkMsg = checkMsg;
	}
	@JSON(serialize=false)
	public PageMode getPage() {
		return page;
	}

	public void setPage(PageMode page) {
		this.page = page;
	}
	
	@JSON(serialize=false)
	public Users getUsers() {
		return users;
	}

	public void setUsers(Users users) {
		this.users = users;
	}
	@JSON(serialize=false)
	public IUsersService getUsersService() {
		return usersService;
	}

	public void setUsersService(IUsersService usersService) {
		this.usersService = usersService;
	}

	// 用户登录
	public String login() throws Exception {
		Users u = this.usersService.queryLogin(this.users);
		if (u == null) {
			this.getRequest().put("msg",
					"<script>alert('帐号或密码错误！');history.go(-1);</script>");
			return "error";
		}
		HttpServletRequest request=ServletActionContext.getRequest();
		// 将登陆用户放入SESSION中
		this.getSession().put("user", u);
		this.getSession().put("loginTime", new MyDate().getDateString());
		this.getSession().put("ip", request.getRemoteAddr());
		
		return "adminIndex";
	}

	// 注销
	public String invo() throws Exception {
		this.getSession().remove("user");
		this.getSession().clear();
		return "index";
	}

	// 根据ID得到用户信息
	public String showDetail() throws Exception {
		Users u = this.usersService.getUsersById(this.users);
		this.getRequest().put("user", u);
		return "showDetail";
	}

	// 得到所有用户信息分页显示
	public String showPage() throws Exception {
		if(this.page==null){
			this.page=new PageMode();
			this.page.setPageIndex(1);
			this.page.setPageSize(10);
		}
		this.page.setAction("users_showPage.action");
		this.page.setModelDrive("page");
		
		page.setSumRows(this.usersService.getUsersCount());
		page.createPageStr();
		this.getRequest().put(
				"usersList",
				this.usersService.getUsersPage(this.page.getPageIndex(),
						this.page.getPageSize()));
		this.getRequest().put("link", page.getPagingStr());
		 this.page=null;
		return "admin";
	}
	// 根据ID得到用户信息
	public String preUpdate() throws Exception {
			this.users.getUsersId();
			Users u = this.usersService.getUsersById(this.users);
			this.getRequest().put("user", u);
			return "updateUsers";
	}
	// 用户信息修改
	public String updateUsers() throws Exception {
		boolean flag = this.usersService.updateUsers(this.users);
		if (!flag) {
			this.getRequest().put("msg",
					"<script>alert('修改失败！');history.go(-1);</script>");
			return "error";
		}
		//更新SESSION
		this.getSession().put("user", this.users);
		return "adminIndex";
	}

	// 添加OA管理用户
	public String addUsers() throws Exception {
		boolean flag = this.usersService.addUsers(this.users);
		if (!flag) {
			this.getRequest().put("msg",
					"<script>alert('添加失败！');history.go(-1);</script>");
			return "error";
		}
		return showPage();
	}

	// 删除用户
	public String deleteUsers() throws Exception {
		boolean flag = this.usersService.deleteUsers(this.users);
		if (!flag) {
			this.getRequest().put("msg",
					"<script>alert('删除失败！');history.go(-1);</script>");
			return "error";
		}
		return showPage();
	}
	//修改用户密码
	public String updatePwd() throws Exception{
		boolean flag=this.usersService.updatePwd(this.users);
		if (!flag) {
			this.getRequest().put("msg",
					"<script>alert('修改失败！');history.go(-1);</script>");
			return "error";
		}
		return "adminIndex";
	}
	
	//判断用户名是否存在
	public String checkName() throws Exception{
		boolean flag=this.usersService.findUsersName(this.users);
		if(flag){
			//不存在
			this.checkMsg="该用户名可以使用";
		}else{
			//存在
			this.checkMsg="该用户名已存在";
		}
		return INPUT;
	}
	
	//筛选用户
	public String selectUsers() throws Exception{
		if(this.page==null){
			this.page=new PageMode();
			this.page.setPageIndex(1);
			this.page.setPageSize(10);
		}
		this.page.setAction("users_selectUsers.action");
		this.page.setModelDrive("page");
		page.setSumRows(this.usersService.getUsersCount(this.users));
		page.createPageStr();
		this.getRequest().put(
				"usersList",
				this.usersService.selectUserLike(this.users,this.page.getPageIndex(),
						this.page.getPageSize()));
		this.getRequest().put("link", page.getPagingStr());
		return "admin";
	}
	//批量删除
	public String deleteMore() throws Exception{
		String idString=ServletActionContext.getRequest().getParameter("idString");
		idString=idString.substring(0,idString.length()-1);
		boolean flag=this.usersService.deleteMore(idString);
		if (!flag) {
			this.getRequest().put("msg",
					"<script>alert('删除失败！');history.go(-1);</script>");
			return "error";
		}
		return showPage();
	}
}
